package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.LeaseAgreementService;
import com.atguigu.lease.web.admin.vo.agreement.AgreementQueryVo;
import com.atguigu.lease.web.admin.vo.agreement.AgreementVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author liubo
 * @description 针对表【lease_agreement(租约信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class LeaseAgreementServiceImpl extends ServiceImpl<LeaseAgreementMapper, LeaseAgreement>
        implements LeaseAgreementService {

    @Autowired
    private   LeaseAgreementMapper  leaseAgreementMapper;

    @Autowired
    private ApartmentInfoMapper  apartmentInfoMapper;

    @Autowired
    private RoomInfoMapper roomInfoMapper;

    @Autowired
    private LeaseTermMapper leaseTermMapper;

    @Autowired
    private PaymentTypeMapper paymentTypeMapper;

    //      根据条件分页查询租约列表
    @Override
    public IPage<AgreementVo> pageAgreementVo(IPage<AgreementVo> ipage, AgreementQueryVo queryVo) {

        IPage<AgreementVo>  agreementVoIPage =   leaseAgreementMapper.pageAgreementVo(ipage , queryVo);

        return agreementVoIPage;
    }

//     根据当前租约id查询租约信息
    @Override
    public AgreementVo getAgreementById(Long id) {

//         根据 租约 id 获取 当前租约对象
        LeaseAgreement leaseAgreement = leaseAgreementMapper.selectById(id);

//        通过当前租约对象获取当前租约的 公寓id
        Long apartmentId = leaseAgreement.getApartmentId();

//        通过当前租约对象 获取 当前租约的 房间id
        Long roomId = leaseAgreement.getRoomId();

//        获取 当前租约信息的 租期id
        Long leaseTermId = leaseAgreement.getLeaseTermId();

//       获取当前 租约的 支付类型id
        Long paymentTypeId = leaseAgreement.getPaymentTypeId();


//      通过 apartmentId（公寓id）  获取 查询当前租约对应的 公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(apartmentId);

//      通过 roomId（房间id）  获取 查询当前租约对应的 房间信息
        RoomInfo roomInfo = roomInfoMapper.selectById(roomId);

//      通过 leaseTermId（租期id）  获取 查询当前租约对应的 租期信息
        LeaseTerm leaseTerm = leaseTermMapper.selectById(leaseTermId);

//      通过 paymentTypeId（支付id）  获取 查询当前租约对应的 支付信息
        PaymentType paymentType = paymentTypeMapper.selectById(paymentTypeId);

//        给 apartmentInfo 赋值

        AgreementVo agreementVo =  new AgreementVo();
//         将 leaseAgreement 和 agreementVo 中 同名的属性 进行赋值
        BeanUtils.copyProperties(leaseAgreement , agreementVo);
        agreementVo.setApartmentInfo(apartmentInfo);
        agreementVo.setRoomInfo(roomInfo);
        agreementVo.setLeaseTerm(leaseTerm);
        agreementVo.setPaymentType(paymentType);
        return agreementVo;
    }


}




